<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>欢迎页面</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="./lib/layui/css/layui.css"  media="all">
    <script src="./lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./js/xadmin.js"></script>
    <script src="./js/jquery.min.js"></script>
</head>
<body>
<form class="layui-form layui-col-space5">
    <div class="layui-inline layui-show-xs-block">
        <input type="text" name="tel" id="tel" placeholder="请输入用户名" autocomplete="off" class="layui-input">
    </div>
    <div class="layui-inline layui-show-xs-block">
        <input type="text" name="sname" id="sname" placeholder="请输入电话" autocomplete="off" class="layui-input">
    </div>
    <div class="layui-inline layui-show-xs-block">
        <button class="layui-btn" lay-submit="" lay-filter="sreach">
            <i class="layui-icon">&#xe615;</i></button>
    </div>
</form>
<div class="layui-btn-group demoTable">
    <button class="layui-btn" data-type = "add" >
        <i class="layui-icon">&#xe654;</i>
        添加用户
    </button>
</div >
<div class="layui-btn-group demoTable">
    <button class="layui-btn" data-type = "delall" > 批量删除 </button>
</div >
                <table class="layui-hide" id="biao" lay-filter="biao"></table>
</body>
<script>
    layui.use(['table', 'form','layer'],function(){
        var table = layui.table;
        var form=layui.form;
        var layer=layui.layer;
        table.render({
            elem: '#biao'
            ,url:'Search.ao'
            ,limits: [10,20,30]
            ,limit: 10
            ,cols: [[
                {type:'checkbox',width:80, fixed: 'left',templet: '#xuanze'}
                ,{field:'id', width:120, title: 'ID', sort: true}
                ,{field:'username', width:120, title: '用户名'}
                ,{field:'role', width:120, title: '职位',templet: '#role'}
                ,{field:'tele', title: '电话'}
                ,{templet: '#caozuo',minWidth:300,fix:'right',title: '操作'}
            ]]
            ,page: true
        });
       form.on('submit(sreach)',function(data){
            table.reload("biao", {
                page : {
                    curr : 1
                },
                where : data.field
            });
            return false;
        });
        table.on('tool(biao)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('真的删除么',{btn:['确定','取消']}, function(index){
                    $.get("del.ao",{id:data.id},function(result){
                        if(result==1){
                            layer.alert("删除成功", {
                                    icon: 1
                                },
                                function(index) {
                                    layer.close(index);
                                    location.reload();
                                });
                        }else{
                            layer.alert("删除失败", {
                                    icon: 1
                                },
                                function() {
                                    layer.close(index);
                                });
                        }
                    });
                    layer.close(index);
                    return false;
                });
            } else if(obj.event === 'up'){
                var data = obj.data;;
                xadmin.open('修改管理员','./up.html?username='+data.username+'&&tele='+data.tele+'&&id='+data.id+'&&role='+data.role,600,400);
            }
        });
        var $ = layui.$, active = {
            add: function(){
                xadmin.open('添加管理员','./add.html',600,400);
            },
                delall: function(){
                    var checkStatus = table.checkStatus('biao'),nmb=0
                        ,data = checkStatus.data;
                    layer.confirm('真的删除么',{btn:['确定','取消']}, function(index) {
                            for (let i in data) {
                                if(data[i].role==1){
                                    layer.alert("超级管理员无法被删除", {
                                            icon: 1
                                        },
                                        function(index) {
                                            layer.close(index);
                                            location.reload();
                                        });
                                }else{
                                    $.get("del.ao", {id: data[i].id}, function (result) {
                                        nmb+=result;
                                        if(i==data.length-1&&nmb>0){
                                            layer.alert("删除成功", {
                                                    icon: 1
                                                },
                                                function(index) {
                                                    layer.close(index);
                                                    if(nmb>0) {
                                                        location.reload();
                                                    }
                                                });
                                        }
                                        if(i==data.length-1&&nmb==0){
                                            layer.alert("删除失败", {
                                                    icon: 1
                                                },
                                                function(index) {
                                                    layer.close(index);
                                                    if(nmb>0) {
                                                        location.reload();
                                                    }
                                                });
                                        }
                                    });
                                }
                            }
                            if(data.length==0){
                                layer.alert("请选择您要删除的管理员", {
                                        icon: 1
                                    },
                                    function(index) {
                                        layer.close(index);
                                    });
                            }
                    });
                    return false;
                }
        };
        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    })
</script>
   <script type="text/html" id="role">
                  {{#  if(d.role === 1){ }}
                     <span class="layui-badge layui-bg-green">超级管理员</span>
                  {{#  } else { }}
                   <span class="layui-badge">管理员</span>
                  {{#  } }}
            </script>
<script type="text/html" id="caozuo">
    {{# if(d.role==2){ }}
    <a class="layui-btn layui-btn-xs" lay-event="up">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    {{#  } else { }}
    <a class="layui-btn layui-btn-sm"  lay-event="up">超级管理员设置</a>
    {{# } }}
</script>
</html>